import allure
import pytest
import time
from selenium.webdriver.common.by import By
from Utils.logger import Log
from PageObjects.login_page import LoginPage
from PageObjects.addCart_page import addCartPage
from PageObjects.mobile_phone_show_page import showPage
from PageObjects.search_page import searchPage
from PageObjects.settlement_page import settlementPage
from PageObjects.submitOrder_page import submitPage
from Utils.readConfig import Read_Config
from Base import browser_engine
from Utils.readExcel import readEx
from Base.base_page import BasePage

excel = readEx("1.xlsx")
excelData = excel.get_xlsx("login")

# 接受logger实例
getLogger = Log("testCase")
log = getLogger.get_log()

@allure.epic("登录购买商品手机1")
@allure.feature("登录购物")
class TestLogin:
    def setup_class(cls) ->None:
        browser_type = Read_Config().get_info("browserType", "browserName")
        cls.driver = browser_engine.getBrowser(browser_type)  #获取浏览器
        cls.driver.implicitly_wait(5)
        cls.loginPaeg = LoginPage(cls.driver)
        cls.searchPage = searchPage(cls.driver)
        cls.showPage = showPage(cls.driver)
        cls.addCartPage = addCartPage(cls.driver)
        cls.settlementPage = settlementPage(cls.driver)
        cls.submitPage = submitPage(cls.driver)


    def teardown_class(cls):
        time.sleep(2)
        cls.driver.quit()

    # 登录商城
    @allure.story("登录购物商城")
    #@pytest.mark.parametrize("user", sqlConnect().selectLoginInfo("select * from user_pass order by verify_code desc"))
    # @pytest.mark.parametrize("user", YamlReader("../Data/login.yaml").data)
    @pytest.mark.parametrize("user", excelData)
    def test_01_login(self, user):
        '''这是测试登录商城'''
        # self.loginPaeg.login(user["username"], user["password"], user["verify_code"])
        #self.loginPaeg.login(user[0], user[1], user[2])
        print(user, user.get("account"), user.get("password"), user.get("verify_code"), f"**{type(user)}****************")
        self.loginPaeg.login(user.get("account"), user.get("password"), user.get("verity_code"))
        # try:
        #     assert self.loginPaeg.get_title() == user["title"]
        # except Exception as e:
        #     pytest.xfail("标题不一致，断言失败了")

        #搜索手机


    @allure.story("搜索手机")
    def test_02_search(self):
        ''' 这是测试搜索商品 "手机" '''
        self.searchPage.searchPhone("手机")

    #搜索列表
    @allure.story("搜索列表")
    def test_03_show_phone_list(self):
        ''' 这是测试显示搜索"手机的列表" '''
        self.showPage.mp_details()

    #加入购物车
    @allure.story("手机加入购物车")
    def test_04_addcart(self):
        ''' 这是测试加入购物车" '''
        self.addCartPage.addcart()

    #结算
    @allure.story("手机商品结算")
    def test_05_addcart(self):
        ''' 这是测试手机商品结算" '''
        self.settlementPage.settlement()

    # 提交订单
    @allure.story("提交手机订单")
    def test_06_addcart(self):
        ''' 这是测试提交手机订单" '''
        self.submitPage.submit()

if __name__ == "__main__":
    pytest.main(["-sv"])
